package org.groupg.practice.JNotify;

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class JNotifyExample {

    // 需要排除的目录列表
    private static final Set<String> EXCLUDED_DIRS = new HashSet<>();

    static {
        Map<String, String> env = System.getenv();
        for (String key : env.keySet()) {
            String value = env.get(key);
            if (new File(value).isDirectory()) {
                EXCLUDED_DIRS.add(value);
            }else if ("PATH".equals(key)) {
                String[] values = value.split(File.pathSeparator);
                EXCLUDED_DIRS.addAll(Arrays.asList(values));
            }
//            EXCLUDED_DIRS.add(env.get(key));
        }
    }

    @Test
    public void test() throws Exception {
        // 监听的目录
        String watchDir = "/path/to/watch";

        File[] roots = File.listRoots();
        for (File root : roots) {
            JNotify.addWatch(root.getAbsolutePath(), JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED, true, new JNotifyListener() {
            @Override
            public void fileCreated(int wd, String rootPath, String name) {
                handleEvent("FILE_CREATED", rootPath, name);
            }

            @Override
            public void fileDeleted(int wd, String rootPath, String name) {
                handleEvent("FILE_DELETED", rootPath, name);
            }

            @Override
            public void fileModified(int wd, String rootPath, String name) {
                handleEvent("FILE_MODIFIED", rootPath, name);
            }

            @Override
            public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
                handleEvent("FILE_RENAMED", rootPath, newName);
            }

            // 统一处理事件
            private void handleEvent(String eventType, String rootPath, String name) {
                String fullPath = new File(rootPath, name).getAbsolutePath();

                // 检查是否在排除目录中
                if (isExcluded(fullPath)) {
                    System.out.println("Ignored event in excluded directory: " + fullPath);
                    return;
                }

                // 处理事件
                System.out.println(eventType + ": " + fullPath);
            }

            // 检查路径是否在排除目录中
            private boolean isExcluded(String path) {
                for (String excludedDir : EXCLUDED_DIRS) {
                    if (path.startsWith(excludedDir)) {
                        return true;
                    }
                }
                return false;
            }
        });
        }
        // 添加监听
        int watchId = JNotify.addWatch(watchDir, JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED, true, new JNotifyListener() {
            @Override
            public void fileCreated(int wd, String rootPath, String name) {
                handleEvent("FILE_CREATED", rootPath, name);
            }

            @Override
            public void fileDeleted(int wd, String rootPath, String name) {
                handleEvent("FILE_DELETED", rootPath, name);
            }

            @Override
            public void fileModified(int wd, String rootPath, String name) {
                handleEvent("FILE_MODIFIED", rootPath, name);
            }

            @Override
            public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
                handleEvent("FILE_RENAMED", rootPath, newName);
            }

            // 统一处理事件
            private void handleEvent(String eventType, String rootPath, String name) {
                String fullPath = new File(rootPath, name).getAbsolutePath();

                // 检查是否在排除目录中
                if (isExcluded(fullPath)) {
                    System.out.println("Ignored event in excluded directory: " + fullPath);
                    return;
                }

                // 处理事件
                System.out.println(eventType + ": " + fullPath);
            }

            // 检查路径是否在排除目录中
            private boolean isExcluded(String path) {
                for (String excludedDir : EXCLUDED_DIRS) {
                    if (path.startsWith(excludedDir)) {
                        return true;
                    }
                }
                return false;
            }
        });

        System.out.println("Watching directory: " + watchDir);

        // 保持程序运行
        Thread.sleep(50*1000);
    }
}